<?php
namespace app\config;
use OSS\Core\OssException;
use OSS\OssClient;

//阿里云OSS   基础设置中 读写权限 请设置为 公共读
define('accessKeyId', 'LTAI5tBuzJfsansBsfwNzxGR');
define('accessKeySecret', 'Fdie4qt663ExByf2dkUaqZ2dtOdSv0');
define('Bucket', 'shopysjz');
define('EndPoint', 'oss-cn-beijing.aliyuncs.com');
class Alioss{
    /**
     * OSS上传本地文件内容
     * @param string $savename 上传的文件名称
     * @param string $filepath 本地文件路径
     * @return null
     */
    public function ossUpload($filepath,$savename)
    {
        try {
            $ossClient = new OssClient(accessKeyId, accessKeySecret, EndPoint);
            $result = $ossClient->uploadFile(Bucket, $savename, $filepath);
        } catch (OssException $e) {
            returnData(500,$e->getMessage());
        }
        return $result;
    }

    /**
     * OSS base64图片上传
     * @param $content
     * @return null
     * @throws OssException
     */
    public function ossBaseImgUpload($content,$imgPath = '',$limitSize = 1){
        $accessKeyId = accessKeyId;
        $accessKeySecret = accessKeySecret;
        $endpoint = EndPoint;
        $bucket = Bucket;
        $imgPath = $imgPath??'image/'.date('Ymd').'/';
        //匹配出图片的格式
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $content, $result)){

            $type = $result[2];
            $new_img_name = date('YmdHis',time()) . mt_rand(100, 999) . ".$type";
            $new_img_name = $imgPath.$new_img_name;
            $img = array('jpg','jpeg','png','gif');//允许上传图片格式
            if(!in_array($type,$img)) returnData(400, '图片格式上传错误');
            //限制图片大小
            $base_imgs = explode(',',$content);
            $img_len = strlen($base_imgs['1']);
            $file_size = $img_len - ($img_len/8)*2;
            $file_size = number_format(($file_size/1024/1024),2); //MB
            if($file_size>$limitSize) returnData(400,'图片大于'.$limitSize.'MB,请上传小于'.$limitSize.'MB的图片');
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, true);
            try{
                $res = $ossClient->putObject($bucket,$new_img_name,base64_decode($base_imgs[1]));
            }catch (OssException $e){
                returnData(400,$e->getMessage());
            }
            if ($res['info']['http_code'] == 200){
                return $new_img_name;
            }

        }
        returnData(500, '图片上传失败');
    }

    /**
     * OSS删除文件
     * @param $file
     */
    public function ossDelFile($file){
        if(empty($file)){
            returnData(500,'删除文件不能空');
        }
        $accessKeyId = accessKeyId;
        $accessKeySecret = accessKeySecret;
        $endpoint = EndPoint;
        $bucket = Bucket;
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            var_dump($ossClient->deleteObject($bucket, $file));
        } catch(OssException $e) {
            returnData(400,$e->getMessage());
        }
    }

    /**
     * OSS 判断文件是否存在
     * @param $file
     * @return bool
     */
    public function ossIsExist($file){
        if(empty($file)){
            returnData(500,'查询文件不能空');
        }
        $config = ossConfig(false);
        $accessKeyId = $config['accessKey'];
        $accessKeySecret = $config['secretKey'];
        $endpoint = $config['domain'];
        $bucket = $config['bucket'];
        try{
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $exist = $ossClient->doesObjectExist($bucket, $file);
        } catch(OssException $e) {
            returnData(400,$e->getMessage());
        }
        return $exist;
    }
}

